set more off
clear 
clear mata
set matsize 4000

cd $WIdata

capture log close
log using "${logsave}/clinicclean_$S_DATE.log", replace
capture set mem 5g


use "${dtasave}/abortionrates_allstates.dta", replace

bysort state_fips: egen max_year = max(year)
bysort state_fips: egen min_year = min(year)
keep if min_year == 2009
keep if max_year == 2017
levelsof state_fips, local(statelist) 
tempfile dataset
save `dataset'
foreach x of varlist abortion_rate {
capture log close
log using "${logsave}/WIabortions_`x'_${S_DATE}.log", replace
use `dataset', clear
preserve
tsset state_fips year
local begyear 2009
local vmatrix "0.25 0.25 0.25 0.25 0.25"
local synthspec "`x'  `x'(2009) `x'(2010) `x'(2011) `x'(2012)  "

local endyear 2017


   #delimit ;
	set more off;
	tempfile synth_`x';
	synth 	`synthspec', nested
			trunit(55) trperiod(2013)
			mspeperiod(`begyear'(1)2012) resultsperiod(`begyear'(1)`endyear')
			keep(`synth_`x'') replace
			fig;
			mat list e(V_matrix);
			mat list e(W_weights);
			ereturn clear;
	#delimit cr
	
	use `synth_`x'', clear
	g time = _time
	graph twoway (scatter _Y_treated time, connect(l) xline(2013.5, lpattern(dash) lcolor(gray)) xline(2015.5, lpattern(dash) lcolor(gray)) ms(none) ) ///
				(scatter _Y_synthetic time, connect(l) clpattern(dash) ms(none)) ///
	, name("graph`x'", replace) xtitle("Month-Year")  scheme(s1mono) ytitle("Abortions Per 1000 Women") `graphoptions' ///
	 legend( order(1 "Wisconsin" 2 "Synthetic Control") rows(2))

	graph export "WI_twoline_synth_`x'_nocontrols.png", replace



keep _Y_treated _Y_synthetic _time
rename _Y_treated Y_1
rename _Y_synthetic Y_0
rename _time year
drop if Y_1 == .
reshape long Y_, i(year) j(group)
g post = year >= 2013

reg Y i.group##post, robust

restore
*******************************************
** Figure 8: Permutation-based inference **
*******************************************
* This code will apply the same synthetic model used earlier to all states in donor pool
#delimit;
set more off;
* This code will apply the same synthetic model used earlier to all states in donor pool
#delimit;
set more off;
// Month Year 1: 106
local synthspec_2010 "`x'   `x'(2009)  ";
local synthspec_2011 "`x'  `x'(2009) `x'(2010)  ";
local synthspec_2012 "`x' `x'(2009) `x'(2010) `x'(2011) ";
local synthspec_2013 "`x' `x'(2009) `x'(2010) `x'(2011)  `x'(2012) ";
local synthspec_2014 "`x'   `x'(2009) `x'(2010) `x'(2011) `x'(2012) `x'(2013) ";
local synthspec_2015 "`x'   `x'(2009) `x'(2010) `x'(2011) `x'(2012) `x'(2013)  `x'(2014) ";
foreach t in 2010 2011 2012 2013 2014 2015 {;
foreach i of local statelist {;

tsset state_fips year;
local t_1 = `t' -1;
		tempfile synth_`x'`i'_`t';
		synth 	`synthspec_`t'', 
		trunit(`i') trperiod(`t') 
		mspeperiod(`begyear'(1)`t_1') resultsperiod(`begyear'(1)`endyear')
		keep(`synth_`x'`i'_`t'') replace ;
		matrix state`t'_`i' = e(RMSPE);  /*check the V matrix*/
		};

 		foreach i of local statelist {;
 		matrix rownames state`t'_`i'=`i';
 		matlist state`t'_`i', names(rows);
 		};

};


#delimit cr
foreach t in 2010 2011 2012 2013 2014 2015 {
 foreach i of local statelist {
 use `synth_`x'`i'_`t'' ,clear
 	keep _Y_treated _Y_synthetic _time
 	drop if _time==.
	rename _time year
 	rename _Y_treated  treat
 	rename _Y_synthetic counterfact
 	gen gap=treat-counterfact
 	sort year 
	gen statefip=`i'
	gen treatmentyear = `t'
 	save `synth_`x'`i'_`t'', replace
}
}
clear
foreach i of local statelist {
		append using `synth_`x'`i'_2010'
		append using `synth_`x'`i'_2011'
		append using `synth_`x'`i'_2012'
		append using `synth_`x'`i'_2013'
		append using `synth_`x'`i'_2014'
		append using `synth_`x'`i'_2015'
	}

egen gappre_tmp=mean(gap) if year<2013, by(statefip) // mean error in pre-period
egen gappre=mean(gappre_tmp), by(statefip) // mean error in all periods
gen estTE=gap // difference between synthetic state and actual state						
drop *tmp

// Graphing treatment effects by state for permutation graph
*xtset statefip year
replace estTE = . if estTE >10 | estTE < -10
local graph = 1
if `graph' == 1{
twoway line estTE year if statefip==1 & treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==2& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==3& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==4& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==5& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==6& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==7& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==8& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==9& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==10& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///	
	line estTE year if statefip==11& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==12& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==13& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==14& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==15& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==16& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==17& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==18& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==19& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==20& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///	
	line estTE year if statefip==21& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==22& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==23& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==24& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==25& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==26& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==27& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==28& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==29& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==30& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///	
	line estTE year if statefip==31& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==32& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==33& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==34& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==35& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==36& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==37& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==38& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==39& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==40& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///	
	line estTE year if statefip==41& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==42& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==43& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==44& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==45& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==46& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==47& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==48& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==49& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==50& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///	
	line estTE year if statefip==51& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==52& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==53& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==54& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==55& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==56& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==57& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==58& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==59& treatmentyear == 2010, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==1 & treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==2& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==3& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==4& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==5& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==6& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==7& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==8& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==9& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==10& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///	
	line estTE year if statefip==11& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==12& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==13& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==14& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==15& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==16& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==17& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==18& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==19& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==20& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///	
	line estTE year if statefip==21& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==22& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==23& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==24& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==25& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==26& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==27& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==28& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==29& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==30& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///	
	line estTE year if statefip==31& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==32& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==33& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==34& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==35& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==36& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==37& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==38& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==39& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==40& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///	
	line estTE year if statefip==41& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==42& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==43& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==44& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==45& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==46& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==47& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==48& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==49& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==50& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///	
	line estTE year if statefip==51& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==52& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==53& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==54& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==55& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==56& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==57& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==58& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==59& treatmentyear == 2011, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==1 & treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==2& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==3& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==4& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==5& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==6& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==7& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==8& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==9& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==10& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///	
	line estTE year if statefip==11& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==12& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==13& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==14& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==15& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==16& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==17& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==18& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==19& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==20& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///	
	line estTE year if statefip==21& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==22& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==23& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==24& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==25& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==26& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==27& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==28& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==29& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==30& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///	
	line estTE year if statefip==31& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==32& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==33& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==34& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==35& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==36& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==37& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==38& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==39& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==40& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///	
	line estTE year if statefip==41& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==42& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==43& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==44& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==45& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==46& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==47& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==48& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==49& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==50& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///	
	line estTE year if statefip==51& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==52& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==53& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==54& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==55& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==56& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==57& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==58& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==59& treatmentyear == 2012, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==1 & treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==2& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==3& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==4& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==5& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==6& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==7& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==8& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==9& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==10& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///	
	line estTE year if statefip==11& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==12& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==13& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==14& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==15& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==16& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==17& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==18& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==19& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==20& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///	
	line estTE year if statefip==21& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==22& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==23& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==24& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==25& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==26& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==27& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==28& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==29& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==30& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///	
	line estTE year if statefip==31& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==32& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==33& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==34& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==35& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==36& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==37& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==38& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==39& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==40& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///	
	line estTE year if statefip==41& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==42& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==43& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==44& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==45& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==46& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==47& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==48& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==49& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==50& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///	
	line estTE year if statefip==51& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==52& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==53& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==54& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==55& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==56& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==57& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==58& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==59& treatmentyear == 2013, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==1 & treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==2& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==3& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==4& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==5& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==6& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==7& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==8& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==9& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==10& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///	
	line estTE year if statefip==11& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==12& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==13& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==14& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==15& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==16& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==17& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==18& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==19& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==20& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///	
	line estTE year if statefip==21& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==22& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==23& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==24& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==25& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==26& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==27& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==28& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==29& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==30& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///	
	line estTE year if statefip==31& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==32& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==33& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==34& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==35& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==36& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==37& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==38& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==39& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==40& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///	
	line estTE year if statefip==41& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==42& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==43& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==44& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==45& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==46& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==47& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==48& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==49& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==50& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///	
	line estTE year if statefip==51& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==52& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==53& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==54& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==55& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==56& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==57& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==58& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==59& treatmentyear == 2014, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==1 & treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==2& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==3& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==4& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==5& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==6& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==7& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==8& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==9& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==10& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///	
	line estTE year if statefip==11& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==12& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==13& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==14& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==15& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==16& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==17& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==18& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==19& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==20& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///	
	line estTE year if statefip==21& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==22& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==23& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==24& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==25& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==26& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==27& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==28& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==29& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==30& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///	
	line estTE year if statefip==31& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==32& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==33& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==34& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==35& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==36& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==37& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==38& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==39& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==40& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///	
	line estTE year if statefip==41& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==42& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==43& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==44& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==45& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==46& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==47& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==48& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==49& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==50& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///	
	line estTE year if statefip==51& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==52& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==53& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==54& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==55& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==56& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==57& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==58& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==59& treatmentyear == 2015, lw(vthin) lcolor(gray) || ///
	line estTE year if statefip==55 &treatmentyear == 2013, lp(solid)lw(thick)lcolor(black) ///
	graphregion(fcolor(white) lcolor(white)) legend(off) ytitle(Difference from Synthetic Control) ///
	 xline(2012, lpattern(dash) lcolor(gray)) xline(2013.5 2015.5, lcolor(red))
	graph export "WI_synthinference_permutations_`x'.png", replace
}
	
	

	
gen gapsq=gap*gap // squared difference between treatment and counterfactual (Y_i - w Y_syn)^2
egen gapsqpre_tmp=mean(gapsq) if year<treatmentyear, by(statefip treatmentyear) // mean square error for pre-period
egen meangapsqpre=mean(gapsqpre_tmp), by(statefip treatmentyear) // mean square error  for pre-period (again?)



forvalues g=1/5 {
	gen estTE`g'=estTE if year==treatmentyear-1+`g' // Treatment effect in each period, labelled as scuh
}

collapse estTE* meangapsqpre, by(statefip treatmentyear) // Creates data set with just estimated treatment effect in year following april 2014 and the MSE in pre-period by state

forvalues g=1/5 {
	sum estTE`g' if statefip==55 & treatmentyear == 2013 // mean treatment for TN in each monthyear
	gen biggerTE`g'=abs(estTE`g')>=abs(r(mean)) // indicator for if size of treatment in time g is bigger in magnitude than TN
	gen estTE`g'sq=estTE`g'*estTE`g' // squared treatment effect
	gen estTE`g'sqratio=estTE`g'sq/meangapsqpre // Ratio of squared treatment effect to MSE
	sum estTE`g'sqratio if statefip==55 & treatmentyear == 2013 // mean Ratio of squared treatment effect to MSE for TN
	gen biggerratio`g'=abs(estTE`g'sqratio)>=abs(r(mean)) // indicator for if ratio is larger for other states than 

}
// P-stat for MSE
sum meangapsqpre if statefip==55 & treatmentyear == 2013 // mean MSE
gen smallerMSE=abs(meangapsqpre)<=abs(r(mean)) // indicator for if MSE in pre-period is smaller than other states 
	
	
egen estTEavg=rowmean(estTE1 estTE2 estTE3 estTE4 estTE5) // mean TE over all periods
sum estTEavg if statefip==55 & treatmentyear == 2013 // summ state of ratio for TN
gen biggerTEavg=abs(estTEavg)>=abs(r(mean)) // Indicator for if the average TE for other states is larger than TN
egen estTEsqavg=rowmean(estTE1sq estTE2sq estTE3sq estTE4sq estTE5sq ) // mean squared TE over all periods
gen estTEavgsqratio=estTEsqavg/meangapsqpre // Ratio of squared treatment to MSE, average across all periods
sum estTEavgsqratio if statefip==55 & treatmentyear == 2013  // summ state of ratio for TN
gen biggerratioavg=abs(estTEavgsqratio)>=abs(r(mean)) // Indicator for if the average for other states is larger than TN


egen estTEavg25=rowmean(estTE2 estTE3 estTE4 estTE5 ) // mean TE excluding first period
sum estTEavg25 if statefip==55 & treatmentyear == 2013 // summ state of ratio for TN
gen biggerTEavg25=abs(estTEavg25)>=abs(r(mean)) // Indicator for if the average TE for other states is larger than TN
egen estTEsqavg25=rowmean(estTE2sq estTE3sq estTE4sq estTE5sq )
gen estTEavg25sqratio=estTEsqavg25/meangapsqpre
sum estTEavg25sqratio if statefip==55 & treatmentyear == 2013 
gen biggerratioavg25=abs(estTEavg25sqratio)>=abs(r(mean))

* Generate TE needed to get p-values of 0.05
sum meangapsqpre if statefip==55 & treatmentyear == 2013 
local meangapsqpreTN=r(mean) // Mean of MSE in pre-period for TN
foreach g in 1 2 3 4 5 avg avg25 {
	sum estTE`g'sqratio if statefip~=55|  treatmentyear ~= 2013  // treatment to MSE for all states other than TN
	sum estTE`g'sqratio if (statefip~=55|  treatmentyear ~= 2013 ) & estTE`g'sqratio~=r(max) // treatment to MSE for all states other than TN, excluding the highest one
	local estTEsq`g'ratio2nd=r(max) // Second highest ratio of TE to MSE
	local estneeded`g'P05=sqrt(`estTEsq`g'ratio2nd'*`meangapsqpreTN')
	
	sum estTE`g' if statefip~=55, detail // treatment to MSE for all states other than TN

	local estTE`g'2nd=r(p95) //estimate needed for 95% percentile

}

cap log close
log using "WI_synthinference_nocontrol_`x'", replace
foreach g in 1 2 3 4 5  avg avg25 {
	qui sum estTE`g' if statefip==55 &  treatmentyear == 2013 
	dis "Estimated Treatment Effect, Year `g'"
	dis r(mean)
	qui sum biggerratio`g'
	dis "Two-sided P-value, based on ratio of TE to MSE"
	dis r(mean)	
	
	
}
	sum meangapsqpre if statefip==55 &  treatmentyear == 2013 
	dis "MSE in pre-period for WI, based on TE"
	dis r(mean)	
	qui sum smallerMSE
	dis "P-value, for pre-period MSE"
	dis r(mean)	

cap log close



}

foreach g in 1 2 3 4 5 avg avg25{
g estTEratio`g' = estTE`g' / sqrt(meangapsqpre)
	sum estTEratio`g' if statefip==55 & treatmentyear == 2013 // mean treatment for TN in each monthyear
	gen onesidedratio`g'=(estTEratio`g')<=r(mean) // indicator for if size of treatment in time g is bigger in magnitude than TN
		sum estTE`g' if statefip==55 & treatmentyear == 2013 // mean treatment for TN in each monthyear
	gen onesided`g'=(estTE`g')<=r(mean) // indicator for if size of treatment in time g is bigger in magnitude than TN
	}
		
	preserve

	foreach g in 1 2 3 4 5 avg avg25{
		qui sum estTE`g' if statefip==55 &  treatmentyear == 2013 
	dis "Estimated Treatment Effect, Year `g'"
	dis r(mean)
	
qui sum biggerratio`g'
	dis "Two-sided P-value, based on ratio of TE to MSE"
	dis r(mean)	
	
	 qui sum onesided`g'
	dis " One-sided P-value, based on TE"
		dis r(mean)	
		
		
	 qui sum onesidedratio`g'
	dis " One-sided P-value, based on ratio of TE to MSE"
		dis r(mean)	
	}
	restore
	
